﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

public partial class ProUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["D_ID"] = CMSUser.Current.UserTypeName;
            CmbDisBind();
            InitDate();
        }
    }

    //初始化数据
    void InitDate()
    {
        using (DataClassesDataContext dc = new DataClassesDataContext())
        {
            var pros = from r in dc.ProjectDetails
                       join a in dc.Approal on r.ApprovalNumber equals a.ID
                       where r.Project_ID == Request.QueryString["PID"]
                       select new
                       {
                           PName = r.Project_Name,
                           CID = r.Community_ID,
                           TID = r.Type_ID,
                           PBInfo=r.Project_BasicInfo,
                           PAddress=r.Project_Address,
                           PScope=r.Project_Scope,
                           SObject=r.ServeObiect,
                           SPtime=r.ServerPersontime,
                           CWay = r.ContactWay,
                           Other=r.other,
                           PDlment = r.Project_Development,
                           PCSubsidy=r.Project_CitySubsidy,
                           PDistrict=r.Project_DistrictFund,
                           PFTotal=r.Project_FundsTotal,
                           RDate = r.ReportDate,
                           ADate=a.Approal_Date,
                           ASummary = a.Approal_Summary,
                           AID = a.Approal_ID,
                           id = a.ID
                           
                       };
            foreach (var pro in pros)
            {

                txtProName.Text = pro.PName;
                cmbType.Value = pro.TID.ToString();
                if (pro.CID.Length.Equals(6))
                {
                    CmbDis.Value = pro.CID.ToString().Substring(0, 2);
                    CmbStr.Value = pro.CID.ToString().Substring(0, 4);
                    CmbCom.Value = pro.CID.ToString();
                }
                else if (pro.CID.Length.Equals(4))
                {
                    CmbDis.Value = pro.CID.ToString().Substring(0, 2);
                    CmbStr.Value = pro.CID.ToString();
                    CmbCom.Text = "";
                }
                else if (pro.CID.Length.Equals(2))
                {
                    CmbDis.Value = pro.CID.ToString();
                    CmbStr.Text = "";
                    CmbCom.Text = "";
                }
                txtProBasic.Text = pro.PBInfo;
                txtProAdd.Text = pro.PAddress;
                txtProScope.Text = pro.PScope;
                txtServerObj.Text = pro.SObject;
                txtServerPer.Text = pro.SPtime;
                txtContact.Text = pro.CWay;
                txtOther.Text = pro.Other;
                txtProDev.Text = pro.PDlment;
                txtCityFund.Text = pro.PCSubsidy;
                txtDisFund.Text = pro.PDistrict;
                txtTotalFunds.Text = pro.PFTotal;
                txtReportDate.Value = pro.RDate.ToString().Substring(0, 10);
                txtOperateDate.Text = DateTime.Now.ToShortDateString();
                txtOperateDate.ReadOnly = true;
                txtApprovalDate.Text = pro.ADate.ToString();
                cmbApprovalNum.Value = pro.id;
               // cmbApprovalNum.Text = pro.AID;
                mmMark.Text = pro.ASummary;
            }
        }
    }

    //绑定城区信息
    private void CmbDisBind()
    {
        //判断用户是否已经登录
        try
        {
            if (!CMSUser.Current.IsLogin)
            {
                Response.Redirect("~/Login.aspx");
            }
        }
        catch (Exception)
        {
            Response.Redirect("~/Login.aspx");
        }
        //获取当前登录的用户
        string UserName = CMSUser.Current.Name;
        if (!UserName.Equals("Administrator"))
        {
            CmbDis.DataSourceID = "SDSDis";
            CmbDis.DataBind();
            CmbDis.ReadOnly = true;
        }
        if (UserName.Equals("Administrator"))
        {
            CmbDis.DataSourceID = "SDSDisAll";
            CmbDis.DataBind();
        }
    }

    //获取街道信息
    protected void CmbStr_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        CmbCom.Items.Clear();
        FillStrCombo(e.Parameter);
    }
    protected void FillStrCombo(string dis)
    {
        if (string.IsNullOrEmpty(dis)) return;
        SDSStr.SelectParameters[0].DefaultValue = dis;
        CmbStr.DataBind();
    }
    //获取社区信息
    protected void cmdCom_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (e.Parameter == "xxx")
        {
            CmbCom.Items.Clear();
        }
        else
        {
            FillComCombo(e.Parameter);
        }
    }
    protected void FillComCombo(string str)
    {
        if (string.IsNullOrEmpty(str)) return;
        SDSCom.SelectParameters[0].DefaultValue = str;
        CmbCom.DataBind();
    }
    // 获取批文号
    protected void cmbApprovalNum_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        FillAppNumCombo(e.Parameter);
    }
    protected void FillAppNumCombo(string str)
    {
        if (string.IsNullOrEmpty(str)) return;
        SDSAppNum.SelectParameters[0].DefaultValue = str;
        cmbApprovalNum.DataBind();
    }

    //根据批文编号读取出批准时间及批文摘要
    protected void lbMark_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        using (DataClassesDataContext dc = new DataClassesDataContext())
        {
            Approal ap = dc.Approal.FirstOrDefault(a => a.ID == e.Parameter);
            cmbApprovalNum.Value = e.Parameter;
            //mmMark.Text = ap.Approal_Summary;
            mmMark.Text = ap.Approal_Summary.Replace("\n\r", "").Trim();
            txtApprovalDate.Text = ap.Approal_Date.ToString();
        }
    }

    //更新数据
    private void UpdatePro()
    {
        using (DataClassesDataContext dc = new DataClassesDataContext())
        {
            var pro = dc.ProjectDetails.Select(p => p)
                   .Where(p => p.Project_ID == Request.QueryString["PID"])
                   .SingleOrDefault();
            pro.Project_Name = txtProName.Text;
            pro.Type_ID = Convert.ToInt32(cmbType.Value.ToString());
            if (!CmbStr.Text.Equals("") && CmbCom.Text.Equals(""))
            {
                pro.Community_ID = CmbStr.Value.ToString();
            }
            else if (!CmbCom.Text.Equals("") && !CmbStr.Text.Equals(""))
            {
                pro.Community_ID = CmbCom.Value.ToString();
            }
            else
            {
                pro.Community_ID = CmbDis.Value.ToString();
            }
            pro.Project_BasicInfo = txtProBasic.Text;
            pro.Project_Address = txtProAdd.Text;
            pro.Project_Scope = txtProScope.Text;
            pro.ServeObiect = txtServerObj.Text;
            pro.ServerPersontime = txtServerPer.Text;
            pro.ContactWay = txtContact.Text;
            pro.other = txtOther.Text;
            pro.Project_Development = txtProDev.Text;
            pro.Project_CitySubsidy = txtCityFund.Text;
            pro.Project_DistrictFund = txtDisFund.Text;
            pro.Project_FundsTotal = txtTotalFunds.Text;
            pro.OperateDate = Convert.ToDateTime(txtOperateDate.Text);
            pro.ReportDate = Convert.ToDateTime(txtReportDate.Text);
            //pro.ApprovalDate = txtApprovalDate.Text.ToString();
            pro.ApprovalNumber = cmbApprovalNum.Value.ToString();
            //pro.Mark = mmMark.Text;
            dc.SubmitChanges();
            Frame.Global.GlobalFunctions.Alert("项目信息修改成功！");
        }
    }

    //确认修改
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtProName.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请输入项目名称！");
        }
        else if (cmbType.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请选择项目类型！");
        }
        else if (cmbApprovalNum.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请选择批文号！");
        }
        else if (txtApprovalDate.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请选择批文号！");
        }
        else if (txtReportDate.Text.Equals(""))
        {
            Frame.Global.GlobalFunctions.Alert("请输入申报时间！");
        }
        else
        {
            UpdatePro();
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ShowProject.aspx");
    }
}